using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces.Orders;
using UwinEducation.Application.Dtos.Orders;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 订单折扣管理控制器
    /// </summary>
    /// <remarks>
    /// 该控制器提供了订单折扣信息的增删改查功能，
    /// 包括查询订单折扣详情、获取订单的折扣列表、创建更新删除订单折扣等操作
    /// </remarks>
    [Authorize]
    //[RequirePermission("student.order")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class OrderDiscountController : ControllerBase
    {
        private readonly IOrderDiscountService _orderDiscountService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="orderDiscountService">订单折扣服务接口</param>
        public OrderDiscountController(IOrderDiscountService orderDiscountService)
        {
            _orderDiscountService = orderDiscountService;
        }

        /// <summary>
        /// 根据ID获取订单折扣详情
        /// </summary>
        /// <remarks>
        /// 根据订单折扣ID查询详细信息，包括订单编号、折扣金额等信息
        /// </remarks>
        /// <param name="request">ID请求参数</param>
        /// <returns>订单折扣详情</returns>
        [HttpPost]
        public async Task<ApiResponse<OrderDiscountDto>> GetById([FromBody] IdRequest request)
        {
            var discount = await _orderDiscountService.GetByIdAsync(request.Id);
            return ApiResponse<OrderDiscountDto>.Ok(discount);
        }

        /// <summary>
        /// 根据订单ID获取折扣列表
        /// </summary>
        /// <remarks>
        /// 获取指定订单的所有折扣信息，用于显示订单的折扣明细
        /// </remarks>
        /// <param name="request">订单ID请求参数</param>
        /// <returns>订单折扣列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<OrderDiscountDto>>> GetByOrderId([FromBody] IdRequest request)
        {
            var discounts = await _orderDiscountService.GetByOrderIdAsync(request.Id);
            return ApiResponse<List<OrderDiscountDto>>.Ok(discounts);
        }

        /// <summary>
        /// 获取订单折扣分页列表
        /// </summary>
        /// <remarks>
        /// 根据筛选条件获取分页的订单折扣信息，支持按金额范围、时间等条件筛选
        /// </remarks>
        /// <param name="request">分页请求参数</param>
        /// <returns>订单折扣分页列表</returns>
        //[HttpPost]
        //public async Task<ApiResponse<PagedResult<OrderDiscountDto>>> GetPagedList([FromBody] OrderDiscountPageRequest request)
        //{
        //    var result = await _orderDiscountService.GetPagedListAsync(request);
        //    return ApiResponse<PagedResult<OrderDiscountDto>>.Ok(result);
        //}

        /// <summary>
        /// 创建订单折扣
        /// </summary>
        /// <remarks>
        /// 为订单创建新的折扣，可以为同一订单创建多个折扣项目
        /// </remarks>
        /// <param name="createDto">创建订单折扣DTO</param>
        /// <returns>创建的订单折扣</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<OrderDiscountDto>> Create([FromBody] CreateOrderDiscountDto createDto)
        {
            var discount = await _orderDiscountService.CreateAsync(createDto);
            return ApiResponse<OrderDiscountDto>.Ok(discount);
        }

        /// <summary>
        /// 批量创建订单折扣
        /// </summary>
        /// <remarks>
        /// 为订单批量创建多个折扣项目，适用于一次性添加多种折扣类型的场景
        /// </remarks>
        /// <param name="createDtos">创建订单折扣DTO列表</param>
        /// <returns>创建的订单折扣列表</returns>
        //[HttpPost]
        //public async Task<ApiResponse<List<OrderDiscountDto>>> CreateRange([FromBody] List<CreateOrderDiscountDto> createDtos)
        //{
        //    var discounts = await _orderDiscountService.CreateRangeAsync(createDtos);
        //    return ApiResponse<List<OrderDiscountDto>>.Ok(discounts);
        //}

        /// <summary>
        /// 更新订单折扣
        /// </summary>
        /// <remarks>
        /// 更新现有订单折扣的信息，如折扣金额、描述等
        /// </remarks>
        /// <param name="updateDto">更新订单折扣DTO</param>
        /// <returns>更新后的订单折扣</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<OrderDiscountDto>> Update([FromBody] UpdateOrderDiscountDto updateDto)
        {
            var discount = await _orderDiscountService.UpdateAsync(updateDto);
            return ApiResponse<OrderDiscountDto>.Ok(discount);
        }

        /// <summary>
        /// 删除订单折扣
        /// </summary>
        /// <remarks>
        /// 删除指定ID的订单折扣
        /// </remarks>
        /// <param name="request">ID请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _orderDiscountService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(null, "删除成功");
        }

        /// <summary>
        /// 批量删除订单折扣
        /// </summary>
        /// <remarks>
        /// 批量删除多个订单折扣
        /// </remarks>
        /// <param name="ids">订单折扣ID列表</param>
        /// <returns>操作结果</returns>
        //[HttpPost]
        //public async Task<ApiResponse<object>> DeleteRange([FromBody] List<Guid> ids)
        //{
        //    await _orderDiscountService.DeleteRangeAsync(ids);
        //    return ApiResponse<object>.Ok(null, "批量删除成功");
        //}

        /// <summary>
        /// 计算订单总折扣金额
        /// </summary>
        /// <remarks>
        /// 计算指定订单的所有折扣项目的总金额
        /// </remarks>
        /// <param name="request">订单ID请求参数</param>
        /// <returns>总折扣金额</returns>
        [HttpPost]
        public async Task<ApiResponse<decimal>> CalculateTotalDiscount([FromBody] IdRequest request)
        {
            var totalDiscount = await _orderDiscountService.CalculateTotalDiscountAsync(request.Id);
            return ApiResponse<decimal>.Ok(totalDiscount);
        }
    }
} 